﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class DiffCountRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15301;
        protected GridView gvDiffCountReport;
        protected GridView gvDiffCountReportShowLot;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnShowLot;
        protected HtmlHead Head1;
        protected Label lblDiffCountReportList;
        protected Label lblMsg;
        protected Label lblTotalInvQty;
        protected Label lblTotalInvQtyText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalInvQty;
        protected UpdatePanel up1;

        public void BindData2gvDiffCountReport(DiffCountRptQueryEntity diffCountRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(diffCountRptQueryEntity.CurrentPage);
            List<DiffCountRptInfo> DiffCountRptInfo = new List<DiffCountRptInfo>();
            DiffCountRptInfo = AnalyticsReportServiceClientProxy.GetInstance().GetDiffCountReportList(diffCountRptQueryEntity);
            if (diffCountRptQueryEntity.ShowLot == "Y")
            {
                base.BindData2GridView(this.gvDiffCountReportShowLot, DiffCountRptInfo);
            }
            else
            {
                base.BindData2GridView(this.gvDiffCountReport, DiffCountRptInfo);
            }
            if ((DiffCountRptInfo != null) && (DiffCountRptInfo.Count > 0))
            {
                this.PagerFooter.PageInfo = DiffCountRptInfo[0].CURRENTPAGE + " / " + DiffCountRptInfo[0].TOTALPAGESCOUNT + "&nbsp;&nbsp;&nbsp;共: " + DiffCountRptInfo[0].TOTALRECORDSCOUNT;
                this.PagerFooter.TotalPages = int.Parse(DiffCountRptInfo[0].TOTALPAGESCOUNT);
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            DiffCountRptQueryEntity diffCountRptQueryEntity = this.GetDiffCountRptQueryEntity();
            diffCountRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = diffCountRptQueryEntity.CurrentPage;
            diffCountRptQueryEntity.CurrentPage = "0";
            if (diffCountRptQueryEntity.ShowLot == "Y")
            {
                this.gvDiffCountReportShowLot.Columns[1].Visible = false;
                this.gvDiffCountReportShowLot.AllowSorting = false;
                this.gvDiffCountReportShowLot.ShowFooter = false;
            }
            else
            {
                this.gvDiffCountReport.Columns[1].Visible = false;
                this.gvDiffCountReport.AllowSorting = false;
                this.gvDiffCountReport.ShowFooter = false;
            }
            this.BindData2gvDiffCountReport(diffCountRptQueryEntity);
            diffCountRptQueryEntity.IsGetAll = false;
            diffCountRptQueryEntity.CurrentPage = currentPageIndex;
            this.SetDiffCountRptQueryEntity(diffCountRptQueryEntity);
            if (diffCountRptQueryEntity.ShowLot == "Y")
            {
                base.Export2Excel("DiffCountRpt.xls", this.gvDiffCountReportShowLot);
            }
            else
            {
                base.Export2Excel("DiffCountRpt.xls", this.gvDiffCountReport);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            DiffCountRptQueryEntity diffCountRptQueryEntity = this.GetDiffCountRptQueryEntity();
            diffCountRptQueryEntity.CurrentPage = "0";
            this.BindData2gvDiffCountReport(diffCountRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            DiffCountRptQueryEntity diffCountRptQueryEntity = this.GetDiffCountRptQueryEntity();
            diffCountRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2gvDiffCountReport(diffCountRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            DiffCountRptQueryEntity diffCountRptQueryEntity = this.GetDiffCountRptQueryEntity();
            diffCountRptQueryEntity.OwnerID = "";
            diffCountRptQueryEntity.SkuID = "";
            diffCountRptQueryEntity.SkuClassID = "";
            diffCountRptQueryEntity.StartLoc = "";
            diffCountRptQueryEntity.ZoneID = "";
            diffCountRptQueryEntity.StartTime = "";
            diffCountRptQueryEntity.EndTime = "";
            diffCountRptQueryEntity.ExternalLot = "";
            diffCountRptQueryEntity.VendorID = "";
            diffCountRptQueryEntity.Lot = "";
            diffCountRptQueryEntity.LPN = "";
            diffCountRptQueryEntity.SkuDescr = "";
            this.BindData2gvDiffCountReport(diffCountRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            DiffCountRptQueryEntity diffCountRptQueryEntity = this.GetDiffCountRptQueryEntity();
            if (int.Parse(diffCountRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                diffCountRptQueryEntity.CurrentPage = (int.Parse(diffCountRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2gvDiffCountReport(diffCountRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            DiffCountRptQueryEntity diffCountRptQueryEntity = this.GetDiffCountRptQueryEntity();
            if (int.Parse(diffCountRptQueryEntity.CurrentPage) > 0)
            {
                diffCountRptQueryEntity.CurrentPage = (int.Parse(diffCountRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2gvDiffCountReport(diffCountRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            DiffCountRptQueryEntity diffCountRptQueryEntity = this.GetDiffCountRptQueryEntity();
            this.BindData2gvDiffCountReport(diffCountRptQueryEntity);
        }

        private DiffCountRptQueryEntity GetDiffCountRptQueryEntity()
        {
            return (base.PageQueryEntitySession as DiffCountRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                DiffCountRptQueryEntity diffCountRptQueryEntity = this.GetDiffCountRptQueryEntity();
                diffCountRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2gvDiffCountReport(diffCountRptQueryEntity);
            }
        }

        protected void gvDiffCountReport_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15301", this.gvDiffCountReport);
        }

        protected void gvDiffCountReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (((Label) e.Row.FindControl("lblInvQty")) != null)
                {
                    this.TotalInvQty += decimal.Parse((((Label) e.Row.FindControl("lblInvQty")).Text != "") ? ((Label) e.Row.FindControl("lblInvQty")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalInvQtyText.Text = base.GetPreferenceDigit(this.TotalInvQty);
            }
        }

        protected void gvDiffCountReport_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                DiffCountRptQueryEntity diffCountRptQueryEntity = this.GetDiffCountRptQueryEntity();
                diffCountRptQueryEntity.SortField = e.SortExpression;
                if (diffCountRptQueryEntity.SortDirection == "ASC")
                {
                    diffCountRptQueryEntity.SortDirection = "DESC";
                }
                else if (diffCountRptQueryEntity.SortDirection == "DESC")
                {
                    diffCountRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2gvDiffCountReport(diffCountRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void gvDiffCountReportShowLot_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15301", this.gvDiffCountReportShowLot);
        }

        private void LoadData()
        {
            DiffCountRptQueryEntity diffCountRptQueryEntity = new DiffCountRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    diffCountRptQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    diffCountRptQueryEntity.SkuClassID = this.PreviousPage.SkuClassID;
                    diffCountRptQueryEntity.SkuID = this.PreviousPage.SkuID;
                    diffCountRptQueryEntity.StartLoc = this.PreviousPage.StartLoc;
                    diffCountRptQueryEntity.ShowLot = this.PreviousPage.ShowLot;
                    diffCountRptQueryEntity.ShowOtherSku = this.PreviousPage.ShowOtherSku;
                    diffCountRptQueryEntity.StartTime = this.PreviousPage.StartTime;
                    diffCountRptQueryEntity.EndTime = this.PreviousPage.EndTime;
                    diffCountRptQueryEntity.ExternalLot = this.PreviousPage.ExternalLot;
                    diffCountRptQueryEntity.VendorID = this.PreviousPage.VendorID;
                    diffCountRptQueryEntity.Lot = this.PreviousPage.Lot;
                    diffCountRptQueryEntity.LPN = this.PreviousPage.Lpn;
                    diffCountRptQueryEntity.SkuDescr = this.PreviousPage.SkuDescr;
                    diffCountRptQueryEntity.ZoneID = this.PreviousPage.ZoneID;
                    if (diffCountRptQueryEntity.ShowLot == "Y")
                    {
                        this.gvDiffCountReportShowLot.Visible = true;
                        this.gvDiffCountReport.Visible = false;
                    }
                }
                else
                {
                    diffCountRptQueryEntity.OwnerID = "";
                    diffCountRptQueryEntity.SkuClassID = "";
                    diffCountRptQueryEntity.SkuID = "";
                    diffCountRptQueryEntity.StartLoc = "";
                    diffCountRptQueryEntity.ShowLot = "";
                    diffCountRptQueryEntity.ShowOtherSku = "";
                    diffCountRptQueryEntity.StartTime = "";
                    diffCountRptQueryEntity.EndTime = "";
                    diffCountRptQueryEntity.ExternalLot = "";
                    diffCountRptQueryEntity.VendorID = "";
                    diffCountRptQueryEntity.Lot = "";
                    diffCountRptQueryEntity.LPN = "";
                    diffCountRptQueryEntity.SkuDescr = "";
                    diffCountRptQueryEntity.ZoneID = "";
                    this.hdnShowLot.Value = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (diffCountRptQueryEntity.ShowLot == "Y")
            {
                diffCountRptQueryEntity.PageSize = this.gvDiffCountReportShowLot.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvDiffCountReportShowLot.PageSize;
            }
            else
            {
                diffCountRptQueryEntity.PageSize = this.gvDiffCountReport.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvDiffCountReport.PageSize;
            }
            diffCountRptQueryEntity.IsGetAll = false;
            diffCountRptQueryEntity.CurrentPage = "0";
            diffCountRptQueryEntity.SortField = "LOC";
            diffCountRptQueryEntity.SortDirection = "DESC";
            this.SetDiffCountRptQueryEntity(diffCountRptQueryEntity);
            this.BindData2gvDiffCountReport(diffCountRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvDiffCountReport);
                base.InitAnalyticsReportGridview(this.gvDiffCountReportShowLot);
                this.LoadData();
            }
        }

        public void SetDiffCountRptQueryEntity(DiffCountRptQueryEntity diffCountRptQueryEntity)
        {
            base.PageQueryEntitySession = diffCountRptQueryEntity;
        }

        public DiffCountRptQuery PreviousPage
        {
            get
            {
                return (DiffCountRptQuery) base.PreviousPage;
            }
        }
    }
}

